/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chatNI;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


/* 
 * @author deepali and vansh
 */
public class ThreadReceiveFile extends Thread {

    /**
     *
     * @param rcvSock It consist of the socket that we use to listen in order to
     * receive the file
     * @param cni consists of the instance of the chat network that we are using
     * to receive this file
     * @param fich Consists of the file that we will create for the received
     * file the file is in the home directory by default
     * @param port consists of the port for this application
     * @param username consists of the user name of the person to whom we will
     * send the file
     *
     */
    private Socket rcvSock;
    private static ChatNI cni;
    private int port;
    private File fich;
    private String username;

    /**
     * This class extends a thread and allows us to start the listening in order
     * to receive a file It accepts a tcp connection request from a client and
     * then receives a file and saves it on the local computer.
     *
     */
    public ThreadReceiveFile(String username, ChatNI cni, int port, String filename) {
        this.username = username;
        this.port = port;
        this.cni = cni;
        String fileaddress = System.getProperty("user.home") + "/" + filename;
        System.out.println("fileaddress :" + fileaddress);
        fich = new File(fileaddress);
    }

    public void run() {
        System.out.println("In ThreadReceiveFile");
        BufferedOutputStream output = null;
        BufferedInputStream input = null;
        try {
            ServerSocket srvSck = null;
            srvSck = new ServerSocket(port);
            System.out.println("Opened port:" + port);
            rcvSock = srvSck.accept();
            System.out.println("Connection Accepted at" + port);
            input = new BufferedInputStream(rcvSock.getInputStream());
            output = new BufferedOutputStream(new FileOutputStream(fich));
            byte[] octets = new byte[1024];
            int num = input.read(octets, 0, 1024);
            do {
                output.write(octets, 0, num);
            } while ((num = input.read(octets, 0, 1024)) != -1);

            output.flush();
            output.close();
            srvSck.close();
            rcvSock.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.flush();
                    output.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        cni.fileReceived(username);
    }
}
